package com.stock.demo.framework.ui.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.stock.demo.framework.fun.Functions;
import com.stock.demo.framework.ui.TipType;
import com.stock.demo.framework.ui.view.LoadingDialog;
import com.stock.demo.framework.ui.view.TipToast;

/**
 * Created by XuZhaoyu on 2017/5/15.
 */

public abstract class BaseFragment extends Fragment {

    private View mView;
    private LoadingDialog mLoadingDialog;
    private TipToast mTipToast;
    private Functions mFunctions;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        mView = createFragmentView(inflater, container, savedInstanceState);
        return mView;
    }

    @Override
    public void onResume() {
        super.onResume();
        initView();
        initData();
    }

    public void showTip(TipType type, String text) {
        switch (type) {
            case LOADING:
                if (mLoadingDialog == null) {
                    mLoadingDialog = new LoadingDialog(getActivity());
                }
                mLoadingDialog.setText(text).build();
                break;
            case NONE:
            case ERROR:
            case SUCCESS:
                if (mTipToast == null) {
                    mTipToast = new TipToast(getContext());
                }
                mTipToast.showToast(type, text);
                break;
        }
    }

    public void endLoading() {
        if (mLoadingDialog == null) {
            return;
        }
        mLoadingDialog.dismiss();
    }

    public void setFunctions(Functions functions) {
        mFunctions = functions;
    }

    public Functions getFunctions() {
        return mFunctions;
    }

    public abstract View createFragmentView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState);

    /**
     * 该方法在onViewCreated()中执行，用于绑定控件
     */
    public abstract void initView();

    /**
     * 改方法在onViewCreated（）中执行，用于对数据进行初始化
     */
    public abstract void initData();

}
